<%- include('../../common/header-font-awesome.html') %>
<%- include('../../common/script-underscore.html') %>
<%- include('../../common/script-jquery.html') %>

<style media="all" type="text/css">
    @page {
        size: A4;
    }


    #signature td:hover, #signature td.active {
        border: 1px solid #00A0E9 !important;
    }

    #signature td {
        position: relative;
    }

    #signature td i {
        color: #00A0E9;
        font-size: 24px;
        display: none;
        position: absolute;
        right: 0;
        bottom: 2px;
    }

    #signature td:hover {
        cursor: pointer;
    }

    #signature td.active i {
        display: block;;
    }

    #contract {
        width: 844px;
        height: 1123px;
        margin: 0 auto;
        padding: 0px;
        border: 0px solid #ddd;
        background: #e7e8eb;;
        font-size:16px;
    }

    .editing {
        margin: 20px auto;
        border: 1px solid #859cff !important;
        box-shadow: 1px 1px 5px 1px #859CFF !important;;
        padding: 10px;
    }

    ul {
        list-style: none;
        margin: 0;
        padding: 0;
    }

    ul li {
    }

    body {
        font-family: SimSun;
        background: #f3f3f3;
        font-size: 14px;
        padding: 0;
        margin: 0;
        -webkit-text-size-adjust: 100%;
        background: #e7e8eb;
    }

    table {
        width: 100%;
        border-collapse: collapse;
        border-spacing: 0;
        border: solid #000000;
        border-width: 2px 0px 0px 2px;
        line-height: 1.5;
    }

    td {
        border: solid #000000;
        border-width: 0px 2px 2px 0px;
        white-space: nowrap;
        font-size: 16px;
    }

    table.w50 td {
        width: 50%;
        padding: 10px;
        white-space: nowrap;
        position: relative;
    }

    .split {
        page-break-after: always;
    }

    p {
        line-height: 1.5;
    }

    .fill {
        text-decoration: none;
        border-bottom: 1px solid #c6c6c6; /* #ccc换成链接的颜色 */
        display: inline-block;
        padding-bottom: 1px; /*这里设置你要空的距离*/
        line-height: 1.3;
    }

    h3 {
        font-size: 16px;
        margin: 0;
        line-height: 2;
    }

    .canprint {
        display: none;
    }

    .edit-locking {
        background: #ddd;
        padding: 0px;
    }

    .btn {
        display: block;
        height: 30px;
        width: 95px;
        text-align: center;
        line-height: 30px;
        background: #00a0e9;
        font-size: 12px;
        color: #FFF;
        margin-bottom: 5px;
        text-decoration: none;
        box-shadow: 1px 1px 5px rgba(136, 136, 136, 0.28);
        border-bottom: 1px solid #958c8c;
        padding-left: 12px;
        position: relative;
    }

    #contract.signature_ {
        cursor: url(../../imgs/signature64X64.ico), default;
    }

    .btn i {
        position: absolute;
        left: 7px;
        top: 10px;
    }

    .btn:hover {
        background: #1984e9;
    }

    .print-area {
        width: 846px;
        margin: 0 auto;
        position: relative;
    }

    .btn-group {
        position: fixed;
        top: 90px;
        right: 90px;
        display: none;
    }

    .after {
        /*display: none;*/
    }

    .chapter {
        position: absolute;
        width: 120px;
        right: 0;
        bottom: 0;
    }

    ul .signature {
        width: 150px;
        height: 150px;
        position: absolute;
        top: 0;
        right: 0;
    }

    .btn.disabled {
        pointer-events: none;
        cursor: not-allowed;
        filter: alpha(opacity=65);
        -webkit-box-shadow: none;
        box-shadow: none;
        opacity: .65;
        pointer-events: all !important;
    }
    h1 {
        font-size: 30px;
        margin: 0;
        line-height: 2;
    }
</style>
<style media="print" type="text/css">
    .tools {
        display: none;
    }

    p {
        line-height: 1.5;
        margin: 10px 0;

    }

    #contract {
        width: 100%;
        margin: 0 auto;
        box-shadow: none;
        padding: 10px;
        border: none;
    }

    title {
        display: none;
    }

    body {
        width: 100%;
        font-size: 17px;
    }

    td {
        font-size: 17px !important;
        position: relative;
    }

    ul .signature {
        width: 150px;
        height: 150px;
        position: absolute;
        top: 0;
        right: 0;
    }
    body{padding-top: 0 !important;}
    #contract{    padding: 0; margin: 0;}
</style>
<style media="screen" type="text/css">
    #contract{
        font-size:16px;}
    p {
        line-height: 1.5;
        margin: 10px 0;
    }
</style>
<title></title>
</head>
<body class="sub-body">
<table id="signature" style="position: fixed;display:none;width: 150px;left: 15px;top:15px;    box-shadow: 1px 1px 5px rgba(136, 136, 136, 0.28);
    border: 1px solid #999;z-index: 99;">
    <tr>
        <td style="text-align: center;background: #FFF;border: none;font-size: 18px;">选择签章</td>
    </tr>
    <tr>
        <td style="border: none;border-top: 1px solid #999;">
            <img src="http://127.0.0.1/thumbnail/2017-07/f38366f0a94743d8af148c6aaaac09ae_big.png" style="
    width: 100%;
    ">
            <i class="fa fa-check" aria-hidden="true"></i>
        </td>
    </tr>
    <tr>
        <td style="border: none;border-top: 1px solid #00A0E9;">
            <i class="fa fa-check" aria-hidden="true"></i>
            <img src="http://127.0.0.1/thumbnail/2017-07/f38366f0a94743d8af148c6aaaac09ae_big.png" style="
    width: 100%;
    ">
        </td>
    </tr>
    <tr>
        <td style="border: none;border-top: 1px solid #00A0E9;">
            <i class="fa fa-check" aria-hidden="true"></i>
            <img src="http://127.0.0.1/thumbnail/2017-07/f38366f0a94743d8af148c6aaaac09ae_big.png" style="
    width: 100%;
    ">
        </td>
    </tr>
</table>
<div id="print">
    <div class="panel-body tools" style="width: 1220px;margin: 0 auto;">
        <div class="canprint" style="margin: 5px 0;">

        </div>
        <div class="noprint">

        </div>
    </div>
    <div class="btn-group">
        <a href="javascript:void(0);" hidefocus="true" class="btn btn-primary  print ">
            <i class="fa fa-print" aria-hidden="true"></i> 打印
        </a>
        <a href="./upload.html" hidefocus="true" class="btn btn-primary   upload before" style="">
            <i class="fa fa-cloud-upload" aria-hidden="true"></i> 上传合同
        </a>
        <a href="javascript:void(0)" hidefocus="true" target="_blank" class="btn btn-primary   preview-upload "
           style="display: none;">
            <i class="fa fa-cloud-upload" aria-hidden="true"></i> 预览上传合同
        </a>
        <a href="./upload.html" hidefocus="true" class="btn btn-primary   upload single after" style="">
            <i class="fa fa-cloud-upload" aria-hidden="true"></i> 上传单签合同
        </a>
        <a href="javascript:void(0)" hidefocus="true" target="_blank" class="btn btn-primary single-upload after"
           style="display: none;">
            <i class="fa fa-cloud-upload" aria-hidden="true"></i> 预览单签合同
        </a>
        <a href="./upload.html" hidefocus="true" class="btn btn-primary   upload couple after" style="">
            <i class="fa fa-cloud-upload" aria-hidden="true"></i> 上传双签合同
        </a>
        <a href="javascript:void(0)" hidefocus="true" target="_blank" class="btn btn-primary   couple-upload after"
           style="display: none;">
            <i class="fa fa-cloud-upload" aria-hidden="true"></i> 预览双签合同
        </a>
        <a href="#" target="_blank" hidefocus="true" class="btn btn-primary   upload-preview" style="display: none">预览上传合同</a>
        <a href="javascript:void(0);" hidefocus="true" class="btn btn-primary noprint edit-save" style="display: none;">
            <i class="fa fa-floppy-o" aria-hidden="true"></i> 保存草稿
        </a>
        <a href="javascript:void(0);" hidefocus="true" class="btn btn-primary noprint edit before">
            <i class="fa fa-pencil-square-o" aria-hidden="true"></i> 编辑
        </a>
        <a href="javascript:void(0);" hidefocus="true" class="btn btn-primary noprint  last-edit before">
            <i class="fa fa-envelope-open-o" aria-hidden="true"></i> 打开保存合同
        </a>
        <a href="javascript:void(0);" hidefocus="true" target="_blank" class="btn btn-primary to-pdf">
            <i class="fa fa-file-pdf-o" aria-hidden="true"></i> PDF下载
        </a>
        <a href="javascript:void(0);" hidefocus="true" target="_blank" class="btn btn-primary apply disabled">
            <i class="fa fa-file-pdf-o" aria-hidden="true"></i> 申请签章
        </a>
        <a href="javascript:void(0);" hidefocus="true" target="_blank" class="btn btn-primary authorized disabled">
            <i class="fa fa-file-pdf-o" aria-hidden="true"></i> 授权签章
        </a>
    </div>
    <!--startprint-->
    <div class="panel-body print-area">
        <div id="contract" style="margin:0px auto;position: relative;margin: 0;">
            <h1 class="enableEdit" style="text-align: center;font-family: SimSun;">销售合同</h1>
            <div style="width: 100%;">
                <div style="display: inline-block;float: left;">
                    <h3>
                        供方：上海海鹏化工科技有限公司
                    </h3>

                    <h3>
                        需方：<span class="edit-lock">{{buyer}}</span>
                    </h3>
                </div>

                <div style="display: inline-block;float: right;">
                    <h3 class="">
                        合同编号：<span class="edit-lock">{{contractNO}}</span>
                    </h3>

                    <h3>
                        时间地点：<span class="edit-lock">{{timePlace}}</span>
                    </h3>
                </div>
                <div style="clear:both;"></div>
            </div>
            <p class="enableEdit">
                供需双方经友好协商，达成如下协议：</p>
            <p class="enableEdit">一、货物名称，品质规格，数量，合同金额，交提货时间等
            </p>
            <table style="text-align: center;">
                <thead>
                <tr>
                    <td>货物名称</td>
                    <td>品质规格</td>
                    <td>包装规格</td>
                    <td>数量({{unit}})</td>
                    <td>单价({{priceUnit}})</td>
                    <td>金额(含税)</td>
                    <td>交提货日期</td>
                    <td>备注</td>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><span class="edit-lock">{{goodName}}</span></td>
                    <td><span class="edit-lock">{{quality}}</span></td>
                    <td><span class="edit-lock">{{wrap}}</span></td>
                    <td><span class="edit-lock">{{counts}}</span></td>
                    <td><span class="edit-lock">{{price}}</span></td>
                    <td><span class="edit-lock">{{money}}</span></td>
                    <td><span class="edit-lock">{{deliveryDate}}</span></td>
                    <td><span class="edit-lock">{{description}}</span></td>
                </tr>
                <tr>
                    <td colspan="2">合同金额大写</td>
                    <td colspan="6" style="text-align: left;"><span class="edit-lock">{{contractValue}}</span></td>
                </tr>
                </tbody>
            </table>
            <p class="enableEdit">
                二、质量标准：{{qualityRequire}}，如有质量异议即时提出，逾期供方不承担责任。
            </p>
            <p>
               {{third}}
            </p>
            <p>
               {{fourth}}
            </p>
            <p>
                <span class="enableEdit">五、货款的结算方式：款到发货,</span><span class="fill edit-lock">{{paymentWay}}</span>，<span
                    class="enableEdit">需方于</span><span
                    class="fill edit-lock">{{paymentDate}}</span><span class="enableEdit">{{paymentCondition}}付清全部货款，如需方不能如期付款，应向供方偿付所欠货款利息，以未按时付款金额的每日千分之五计算，供方汇款资料如下:　
                </span><span> 开户行：<b>中国民生银行上海田林支行</b> 账 号：699425653</span>
            </p>
            <p class="enableEdit">
                六、货物属性：如属于危险化学品或易制毒化学品，需方须按规定提供相应的化学品经营或者使用资质，办理该产品易制毒采购证和运输证（如果需要），由于未按规定提供或者办理相应证件所产生的任何损失，费用和责任等均由需方承担。
            </p>
            <p class="enableEdit">
                七、违约责任：任何一方违反本合同，应向对方偿付合同总值20%的违约金。如因违约使对方遭受其它损失，还应赔偿对方遭受的其它损失，但由于不可抗力而造成本合同不能执行，则双方皆不承担违约责任，不可抗力包括但不限于地震，台风，战争，火灾等。
            </p>
            <p class="enableEdit">
                八、纠纷解决：执行合同过程中若发生纠纷，双方应协商解决，协商不成时，可向原告方所在地法院提出诉讼。
            </p>
            <p class="enableEdit">
                九、生效条款：本合同应在当日18:00前双方盖章或签字生效，传真件,扫描件等与原件具有同等法律效力，若有修改，须双方加盖合同章（公章）生效，此前有关本合同的函电，文本若与本合同相抵触，均作无效处理。
            </p>
            <p class="enableEdit">
                十、其它事项：如有未尽事宜，双方协商解决，协商不成,任何一方可以向原告方所在地人民法院提起诉讼。
            </p>
            <p class="enableEdit">
                十一、本合同一式两份，双方各执一份。
            </p>
            <table class="w50" style="border-collapse: inherit;border:1px solid #000000;">
                <tr>
                    <td style="border:none;border-right:1px solid #000000;    vertical-align: top;">
                        <ul style="width:100%;white-space: normal;line-height: 1.3;">
                            <li style="text-align: center;margin-bottom: 1px;">供方</li>
                            <li>单位名称：上海海鹏化工科技有限公司</li>
                            <li>执行代表：{{name}}</li>
                            <li>联系电话：{{tel}}</li>
                            <li>联系传真：{{fax}}</li>
                            <li style="width:320px;display:block;white-space:pre-wrap;word-break: break-all;word-wrap: break-word">联系地址：中国上海市剑川路951号1号楼北楼507室</li>
                        </ul>
                    </td>
                    <td style="border: none;vertical-align: top;">
                        <ul style="width:100%;white-space: normal;line-height: 1.3;" class="signature-table">
                            <li style="text-align: center;margin-bottom: 1px;">需方</li>
                            <li class="edit-lock">单位名称：{{company}}</li>
                            <li class="edit-lock">执行代表：{{representative}}</li>
                            <li class="edit-lock">联系电话：{{contactTel}}</li>
                            <li class="edit-lock">联系传真：{{contactFax}}</li>
                            <li class="edit-lock" style="width:320px;display:block;white-space:pre-wrap;word-break: break-all;word-wrap: break-word">联系地址：{{contactAddress}}</li>
                        </ul>
                    </td>
                </tr>
            </table>
        </div>
        <div></div>
    </div>
    <!--endprint-->
</div>
</body>
<%- include('../../common/script-common.html') %>
<script language="javascript">
    $(function () {

        $(window).resize(resize);
        function resize() {
            $('.btn-group').css({
                top: $('#contract').offset().top,
                right: ($(window).width() - 846) / 2 - 115
            });
            $('.btn-group').fadeIn(1500);
        }

        resize();
        var searchObj = parseSearch();
        $('.upload.before').attr('href', $('.upload.before').attr('href') + '?id=' + searchObj.id);
        $('.upload.single').attr('href', $('.upload.single').attr('href') + '?id=' + searchObj.id + '&type=single');
        $('.upload.couple').attr('href', $('.upload.couple').attr('href') + '?id=' + searchObj.id + '&type=double');
        var paper = {};
        $.get('/order/getOrderPaper?id=' + searchObj.id, function (data) {
            paper = data.data;
            if (paper.checkStatus == 1 && paper.body) {
                $('.edit').hide();
                $('.print-area').html(paper.body); // 获取原来保存合同
//                $('.print-area').removeClass('print-area');
                $('.enableEdit').attr('contenteditable', false);
            }
            else {
                $('.apply').hide();
                $('.authorized').hide();
                $.get('/order/print?id=' + searchObj.id, function (data) {
                    if (data.code == 200) {
                        var obj = data.data;
                        var contract = $('#contract').html();
                        for (var key in obj) {
                            if (obj[key] != null) {
                                if (key == 'deliveryDate') {
                                    contract = contract.replace('{{' + key + '}}', obj[key])
                                } else if (key == 'description') {
                                    contract = contract.replace('{{' + key + '}}', "")
                                } else if (key == 'deliverWay') {
                                    if (obj[key].index("自提") > -1) {
                                        contract = contract.replace('{{' + key + '}}', "需方自提主流库，如有费用需方自理。")
                                    } else {
                                        contract = contract.replace('{{' + key + '}}', "供方负责运输,于2017年3月22日送到供方工厂!")
                                    }
                                } else {
                                    if ('price' == key || 'money' == key) {
                                        console.log(obj[key]);
                                        obj[key] = (parseFloat(obj[key])).toFixed(2);
                                    }
                                    contract = contract.replace('{{' + key + '}}', obj[key])
                                }
                            }
                        }
                        if(obj['deliveryWay'].indexOf('自提')>-1){
                            contract = contract.replace('{{fourth}}', '<span class="enableEdit">四、货物数量结算方式：以出库磅码单为准，免费仓储期至 </span><span class="fill edit-lock">'+obj.freeDate+'</span><span  class="enableEdit">，逾期仓储费和损耗由需方承担。</span><span class="enableEdit">仓储费发票单独开服务发票。</span>');
                            contract = contract.replace('{{third}}', ' <span class="enableEdit">三、提货方式：</span><span class="fill edit-lock">'+obj.deliveryWay+'</span>，如有费用需方自理。');
                        }else {
                            contract = contract.replace('{{fourth}}', '<span class="enableEdit">四、货物数量结算方式：以出库磅码单为准，按国家规定合理损耗为千分之三，损耗在千分之三以内由需方承担，超出千分之三的部分由供方承担；如果需方采购后无需立刻送货，免费仓储期至 </span><span class="fill edit-lock">'+obj.freeDate+'</span><span class="enableEdit">，逾期仓储费和损耗由需方承担。仓储费发票单独开服务发票。</span>');
                            contract = contract.replace('{{third}}', ' <span class="enableEdit">三、提货方式：</span><span class="fill edit-lock">'+obj.deliveryWay.split(",")[0]+'</span>，送货地址:<span class="fill edit-lock">'+obj.deliveryWay.split(",")[1]+'</span>。');
                        }
                        if (obj.status == 0) {
                            $('.after').hide();
                        }
                        if (obj.status == 1) {
                            $('.before').hide();
                            $('.after').css({'display': 'block'});
                            $('.preview-upload').css({'display': 'block'});
                        }

                        $('.to-pdf').click(function () {
                            $.post('/order/save-paper', {
                                orderId: searchObj.id,
                                type: 0,
                                paper: getPaper()
                            }, function (xhr) {
                                if (xhr.code == 200) {
                                    $('.to-pdf').attr('href', "/api/admin/order/" + obj['contractNO'] + "-" + obj["buyer"] + ".pdf?id=" + searchObj.id);
                                    window.location = "/api/admin/order/" + obj['contractNO'] + "-" + obj["buyer"] + ".pdf?id=" + searchObj.id
                                } else {
                                    alert(xhr.error)
                                }
                            })
                        });
                        $('#contract').html(contract);
                    } else {
                        $('#print').html('<div style="width: 500px;text-align: center;margin: 50px auto;background: #b02e2e;font-size: 20px;padding: 50px;color: #FFF;">无权操作,请向超级管理员申请此操作权限!</div>')
                    }
                });
            }
            if (paper.path) {
                $('.preview-upload').attr('href', paper.path);
                $('.preview-upload').show();
            }
            if (paper.single) {
                $('.single-upload').attr('href', paper.single);
                $('.single-upload').css({'display': 'block'});
            }
            if (paper.couple) {
                $('.couple-upload').attr('href', paper.couple);
                $('.couple-upload').css({'display': 'block'});
            }
            $('.to-pdf').click(function () {
                $.post('/order/save-paper', {
                    orderId: searchObj.id,
                    type: 0,
                    paper: getPaper()
                }, function (xhr) {
                    if (xhr.code == 200) {
                        $('.to-pdf').attr('href', "/api/admin/order/" + searchObj['contractNO'] + "-" + searchObj["buyer"] + ".pdf?id=" + searchObj.id);
                        window.location = "/api/admin/order/" + searchObj['contractNO'] + "-" + searchObj["buyer"] + ".pdf?id=" + searchObj.id
                    } else {
                        alert(xhr.error)
                    }
                })
            });
            $('#contract').click(function (e) {
                var $singature = $('.signature1');
                if (window.isChange) {
                    $singature = $('.signature2');
                }
                if ($(this).hasClass('signature_')) {
                    if ($singature.length) {
                        $singature.remove();
                    }
                    if (!$('#signature .active img').length) {
                        return alert('请选择公章或签名!');
                    }
                    var $img = $('<img/> ').attr('src', $('#signature .active img').attr('src')).css({
                        width: '100%'
                    }).addClass('chapter');
                    $(this).append($('<div></div>').addClass('signature' + ($('.chapter').length + 1)).css({
                        position: 'absolute',
                        width: '200px',
                        height: '200px',
                        border: '1px solid gray',
                        left: e.pageX - $(this).offset().left - 100,
                        top: e.pageY - $(this).offset().top - 100,
                    }).append($img).append('<span style=" \
                        position: absolute;\
                        bottom: -1px;\
                        height: 20px;\
                        width: 40px;\
                        border: 1px solid green;\
                        text-align: center;\
                        line-height: 40px;background: #FFF;\
                        cursor: pointer;\
                        left: -1px;\
                        "><i class="fa fa-check"></i></span>').append('<span style=" \
                        position: absolute;\
                        bottom: -1px;background: #FFF;\
                        height: 20px;\
                        width: 40px;\
                        border: 1px solid green;\
                        text-align: center;\
                        line-height: 40px;\
                        cursor: pointer;\
                        left: 38px;\
                        "><i class="fa fa-times"></i></span>'))
                }
                $('.fa-times').unbind('click').click(function (e) {
                    e.stopPropagation();
                    $(this).closest('div').remove()
                });
                $('.fa-check').unbind('click').click(function (e) {
                    e.stopPropagation();
                    if (confirm('确定签章?')) {
                        $('.chapter').closest('.signature' + ($('.chapter').length)).css({
                            border: 'none'
                        }).end().siblings('span').remove().end().closest('span').remove();
                        $('#signature').hide();
                        $('#contract').removeClass('signature_');
                        savePaper("签章成功!", 1);
                    }
                })
            });
            var $authorized = $('.authorized'), $apply = $('.apply');
            disableBtn($authorized);
            disableBtn($apply);
            $.when(getUserPermission("order:"))
                    .then(function (permission) {
                                if (paper.signatureStatus == 0 && permission['order:authorized_signature']) {
                                    disableBtn($apply);
                                    enableBtn($authorized);
                                    onClick($authorized, function () {
                                        $.when(getUserSignature())
                                                .then(function (com) {
                                                    if (com.tel == "1") {
                                                        return fnFail("超级管理员不能授权签章!")
                                                    }
                                                    if (com.signature || com.signaturePaper || com.signatureFinance) {
                                                        signature(com, false)

// $('.signature-table').append('<img class="signature" src="' + com.signature + '">');
//                                                        return savePaper();
                                                    } else {
                                                        alert("请上传公章!")
                                                    }
                                                })
                                                .fail(function (e) {
                                                    console.log(e);
                                                    alert(e.error);
                                                });
                                    })
                                } else if (!paper.signatureStatus && permission['order:apply_signature']) {
                                    enableBtn($apply);
                                    onClick($apply, function () {
                                        $.post('/orderPaper/apply/' + searchObj.id + '/0', {visit:window.location.href},function (res) {
                                            if (res.code == 200) {
                                                fnSuccess('申请成功!');
                                                disableBtn($apply);
                                                window.location = window.location;
                                            } else {
                                                alert('申请成功!');
                                            }
                                        })
                                    })

                                } else {
                                    $apply.hide();
                                    $authorized.hide();
                                }
                            }
                    )
                    .fail(function (e) {
                        console.log(e);
                    });
        });
        /**
         * 签章
         * isChange: 是否进行提单变更
         */
        function signature(com, isChange) {
            $('#signature').find('tr:nth-child(2) img').attr('src', com.signature);
            $('#signature').find('tr:nth-child(3) img').attr('src', com.signaturePaper);
            $('#signature').find('tr:nth-child(4) img').attr('src', com.signatureFinance);
            $('.enableEdit').attr('contenteditable', false);
            $('#signature').show();
        }

        $('#signature').find('td').click(function (e) {
            if ($(this).closest('tr').index() == 0) {
                return;
            }
            if (!$('#signature').find('td').find('img').length || $('#signature').find('td').find('img').attr('src') == 'undefined') {
                return '';
            }
            $('#signature').find('td').removeClass('active');
            $(this).addClass("active");
            $('#contract').addClass('signature_')
        });
        function getPaper() {
            var str = $('.print-area').html();
            var matchers = str.match(/<img[\s\S]*?>/g);
            if (matchers && matchers.length) {
                str = str.replace(matchers[0], matchers[0] + '</img>');
            }
            return str;
        }

        function savePaper(msg, sig) {
            $.post('/order/save-paper', {
                orderId: searchObj.id,
                paper: getPaper(),
                signatureStatus: sig || '',
            }, function (xhr) {
                if (xhr.code == 200) {
                    alert(msg || "保存成功");
                    window.location = window.location;
                    ;
//                    window.open("/api/admin/purchaseOrder/" + paper.com + ".pdf?id=" + _search.id, '_blank');
                } else {
                    fnFail(xhr.error)
                }
            });
        }

        $('.last-edit').click(function () {
            $('.print-area').html(paper.body);
        });

        if (searchObj.type == 'print') {
            $('.canprint').show();
        }


        $('.print').click(function () {
            $('.edit-lock').removeClass("edit-locking");
            $('#contract').removeClass('editing');
            if (getExplorer() == "IE") {
                pagesetup_null();
            }
            pagesetup_default();
            window.print();
        });
        function getPaper() {
            var str = $('.print-area').html();
            var matcher_ = str.match(/<img[\s\S]*?>/g);
            if (matcher_ && matcher_.length) {
                str = str.replace(matcher_[0], matcher_[0] + '</img>');
            }
            return str;
        }

        $('.edit').click(function () {
            $(this).siblings('.edit-save').show();
            $('#contract').addClass('editing');
            $('.edit-lock').addClass("edit-locking");
            $('.enableEdit').attr('contenteditable', true);
            $(this).hide();
        });
        var saving = false;
        $('.edit-save').click(function () {
            if (!saving) {
                $(this).html('保存中...');
                saving = true;
            } else {
                return alert('保存处理中...');
            }
            $('.edit-lock').removeClass("edit-locking");
            var self = this;
            $.post('/order/save-paper', {
                orderId: searchObj.id,
                type: "0",
                paper: getPaper()
            }, function (xhr) {
                saving = false;
                if (xhr.code == 200) {
                    $(self).siblings('.edit').show();
                    $(self).hide();

                    $(self).html('保存').hide();
                    $('.enableEdit').attr('contenteditable', false);
                    $('#contract').removeClass('editing');
                    if (confirm("合同保存成功，是否关闭本页面?!")) {
                        window.close();
                    }
                } else {
                    alert(xhr.error)
                }
            });
        });


        function getExplorer() {
            var explorer = window.navigator.userAgent;
            //ie
            if (explorer.indexOf("MSIE") >= 0) {
                return "IE";
            }
            //firefox
            else if (explorer.indexOf("Firefox") >= 0) {
                return "Firefox";
            }
            //Chrome
            else if (explorer.indexOf("Chrome") >= 0) {
                return "Chrome";
            }
            //Opera
            else if (explorer.indexOf("Opera") >= 0) {
                return "Opera";
            }
            //Safari
            else if (explorer.indexOf("Safari") >= 0) {
                return "Safari";
            }
        }


        function pagesetup_default() {
            try {
                var RegWsh = new ActiveXObject("WScript.Shell")
                hkey_key = "header"
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&w&b页码，&p/&P")
                hkey_key = "footer"
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&u&b&d")

                //以下设置页面边距
                hkey_key = "margin_bottom";
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0");
                hkey_key = "margin_left";
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.1");
                hkey_key = "margin_right";
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.1");
                hkey_key = "margin_top";
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.4");
            } catch (e) {
            }
        }

        var hkey_root, hkey_path, hkey_key
        hkey_root = "HKEY_CURRENT_USER"
        hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
        //设置网页打印的页眉页脚为空
        function pagesetup_null() {
            try {
                var RegWsh = new ActiveXObject("WScript.Shell");
                hkey_key = "header";
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "");
                hkey_key = "footer";
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "")
            } catch (e) {
            }
        }
    })
</script>
</html>